<?php

namespace app\admin\model;

use think\Model;
/**
 * 用户订单类
 */
class RoomsUser extends Model
{
    /**
     * 删除订单
     */
    public function DeleteReservation($id)
    {
        if ($this->where('id', $id)->count() == 1) {
            $this->where('id', $id)->delete();

            return Result(1, '删除成功');
        }

        return Result(0, '提供的数据id不正确');
    }

    /**
     * 修改订单
     */
    public function ModifyReservation($id, $option)
    {
        if ($this->where('id', $id)->count() == 0) {
            return Result(0, '该预约不存在');
        }
        // 获取客房价格
        $option['price'] = $this->where('id', $id)->value('price');
        // 判断客房是否存在
        if (array_key_exists('rooms_id', $option) && !empty($option['rooms_id'])) {
            $rooms = db('rooms')->where('id', $option['rooms_id'])->find();
            if($rooms) {
                $option['price'] = $rooms['price'];
            } else {
                return Result(0, '该客房不存在');
            }
        }
        // 计算总价价格
        if(array_key_exists('start_time', $option) && array_key_exists('end_time', $option)) {
            $option['day'] = round((strtotime($option['end_time']) - strtotime($option['start_time'])) / 3600 / 24);
            $option['price_num'] = $option['day'] * $option['price'];
        }

        $this->where('id', $id)->data($option)->update();

        return Result(1, '修改成功');
    }

    /**
     * 获取单个订单
     */
    public function GetReservation($id)
    {
        if ($this->where('id', $id)->count() == 0) {
            return Result(0, '该预约不存在');
        }

        $info =  db('rooms')
            ->alias('r')
            ->field('r.address, r.name rooms_name, s.name specifications_name')
            ->field('re.name region_name, ru.*, u.name user_name')
            ->join('specifications s', 'r.specifications_id = s.id', 'LEFT')
            ->join('region re', 'r.region_id = re.id', 'LEFT')
            ->join('rooms_user ru', 'ru.rooms_id = r.id', 'LEFT')
            ->join('user u', 'ru.user_id = u.id', 'LEFT')
            ->where('ru.id', $id)
            ->find();

        return Result(1, '', [
            'info' => $info
        ]);
    }

    /**
     * 获取所有订单
     */
    public function GetReservationList($page, $option)
    {
        // 创建匿名函数 方便多次调用 用完自动销毁
        $buildDb = function () use ($option) {
            // 连表查询数据
            $db = $this
                ->alias('ru')
                ->field('r.address, r.name rooms_name, s.name specifications_name')
                ->field('re.name region_name, ru.*, u.name user_name')
                ->field('r.region_id, r.specifications_id, u.mobile user_mobile')
                ->join('rooms r', 'ru.rooms_id = r.id', 'LEFT')
                ->join('specifications s', 'r.specifications_id = s.id', 'LEFT')
                ->join('region re', 'r.region_id = re.id', 'LEFT')
                ->join('user u', 'ru.user_id = u.id', 'LEFT');

            // -start 根据查询条件查询数据
            if (array_key_exists('start_time', $option) && array_key_exists('end_time', $option)) {
                $db = $db
                    ->whereTime('ru.start_time', '>=', $option['start_time'])
                    ->whereTime('ru.end_time', '<=', $option['end_time']);
            }
            if (array_key_exists('user_id', $option) && !empty($option['user_id'])) {
                $db = $db->where('ru.user_id', $option['user_id']);
            }

            if (array_key_exists('state', $option)){
                $db = $db->where('ru.state', $option['state']);
            }

            if (array_key_exists('user_mobile', $option) && !empty($option['user_mobile'])){
                $db = $db->where('ru.mobile', $option['user_mobile']);
            }

            if (array_key_exists('rooms_mobile', $option) && !empty($option['rooms_mobile'])){
                $db = $db->where('r.mobile', $option['rooms_mobile']);
            }

            if (array_key_exists('rooms_id', $option) && !empty($option['rooms_id'])){
                $db = $db->where('ru.rooms_id', $option['rooms_id']);
            }
            // -end
            return $db;
        };

        $count = $buildDb()->count();
        $list = $buildDb()->order('ru.id', 'DESC')->page($page, 20)->select();

        return Result(1, '', [
            'count' => $count,
            'pageSize' => 20,
            'list' => $list
        ]);
    }
}
